//
//  ImageCell.swift
//  YueChat
//
//  Created by IMac on 2024/9/23.
//

import UIKit

class ImageCell: BaseChatCell{

    @IBOutlet weak var showImgHeight: NSLayoutConstraint!
    @IBOutlet weak var showImgWidth: NSLayoutConstraint!
    @IBOutlet weak var showImg: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func baseCellHeight(message: DMCCMessage, chatType: Bool, mulSelect: Bool, isMySend: Bool) {
        var size = CGSize(width: 80, height: 143)
        let content = message.content as? OsnImageMessageContent
        if content != nil {
            if message.direction == .Send {
                size = getImageSizeForArea(msgModel: message, withViewWidth: UIScreen.main.bounds.width - 120)
            }
        } else {
            size = CGSize(width: 80, height: 143)
        }
        message.msgCellWidth = Int(size.width)
        message.msgCellHeight = Int(size.height + 20)
    }

    // 设置消息
    override func setMessage(_ message: DMCCMessage) {
        super.setMessage(message)

        let homeDirectoryURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
        var homeStr = homeDirectoryURL?.path
        homeStr = formatLocalUrl(filePath: homeStr!)
//        self.headImgBG.image = UIImage(named: "member_manager")
        if let imgContent = message.content as? OsnImageMessageContent {
            imgContent.localPath = getHomeFilePath(localPath: imgContent.localPath)
            var size = CGSize(width: 80, height: 143)
            if message.direction == .Send {
                if imgContent.localPath.count > 0 {
                    
//                    self.showImg.image = UIImage(named: imgContent.localPath)//UIImage(named: imgContent.localPath)
                    self.showImg.sd_setImage(with: URL(fileURLWithPath: imgContent.localPath), placeholderImage: UIImage(named: "chatImgBg"))
                } else {
                    if imgContent.decKey.count > 0 {
                        self.showImg.image = UIImage(named: "deckey")
                    } else if imgContent.remoteUrl.hasSuffix(".zip") && imgContent.decKey.count == 0 {
                        self.showImg.image = UIImage(named: "file_damage")
                    } else {
                        self.showImg.image = UIImage(named: "chatImgBg")
                    }
                }
                size = getImageSizeForArea(msgModel: message, withViewWidth: UIScreen.main.bounds.width - 120)

            } else {
                self.nickNameLabel.snp.makeConstraints { make in
                    make.left.equalTo(self.headImg.snp_rightMargin)
                    make.right.equalTo(self.contentView).offset(-50)
                    make.top.equalTo(self.headImg)
                }
                
                if imgContent.decKey.count == 0 {
                    imgContent.localPath = ""
                }

                if imgContent.localPath.count > 0 {
                    if imgContent.localPath.contains(homeStr!) {
                        self.showImg.image = UIImage(named: imgContent.localPath)
                    } else if imgContent.localPath.hasSuffix(".png") {
                        self.showImg.image = UIImage(named: imgContent.localPath)
                    }else {
                        if let url = URL(string: imgContent.remoteUrl) {
                            self.showImg.sd_setImage(with: url, placeholderImage: UIImage(named: "chatImgBg"))
                        }
                    }
                } else {
                    if let url = URL(string: imgContent.remoteUrl) {
                        self.showImg.sd_setImage(with: url, placeholderImage: UIImage(named: "chatImgBg"))
                    }

                    if imgContent.remoteUrl.hasSuffix(".zip") && imgContent.decKey.count > 0 {
                        self.showImg.image = UIImage(named: "deckey")
                    } else if imgContent.remoteUrl.hasSuffix(".zip") && imgContent.decKey.count == 0 {
                        self.showImg.image = UIImage(named: "file_damage")
                    }
                }
            }
            
            self.showImgWidth.constant = size.width
            self.showImgHeight.constant = size.height
        } else if let stickerContent = message.content as? OsnStickerMessageContent {
            if message.direction == .Send {
                self.showImg.image = UIImage(named: getFilePath(localPath: stickerContent.localPath))
            } else {
                if stickerContent.localPath.count > 0 {
                    if stickerContent.localPath.contains(homeStr!) {
                        self.showImg.image = UIImage(named: stickerContent.localPath)
                    } else {
                        self.showImg.sd_setImage(with: URL(string: stickerContent.remoteUrl), placeholderImage: UIImage(named: "failPhoto"))
                    }
                } else {
                    self.showImg.sd_setImage(with: URL(string: stickerContent.remoteUrl), placeholderImage: UIImage(named: "failPhoto"))
                }
            }
            
            let size = CGSize(width: 80, height: 143)
            self.showImgWidth.constant = size.width
            self.showImgHeight.constant = size.height
        }
        
//        if message.direction == 0 {
//            self.headImg.sd_setImage(with: URL(string: self.myInfo.portrait), placeholderImage: SDImageDefault)
//            let nftStr = myInfo.getNft()
//            self.nftImg.isHidden = nftStr.isEmpty
//        } else {
//            if chatType == 1 {
//                if message.originalUser.count > 0 {
//                    if let userInfo = gosnUser.getUserInfo(message.originalUser, inGroup: message.target, refresh: false) {
//                        self.headImg.sd_setImage(with: URL(string: userInfo.portrait), placeholderImage: SDImageDefault)
//                        
//                        self.nftImg.isHidden = userInfo.getNft().isEmpty
//                    }
//                } else {
//                    self.headImg.sd_setImage(with: URL(string: self.userInfo.portrait), placeholderImage: SDImageDefault)
//                }
//            } else {
//                self.headImg.sd_setImage(with: URL(string: self.userInfo.portrait), placeholderImage: SDImageDefault)
//            }
//        }
        
        
//        self.errorImg.snp.makeConstraints { make in
//            make.right.equalTo(self.showImg.snp.left).offset(-15)
//            make.centerY.equalTo(self.headImg.snp.centerY)
//            make.width.height.equalTo(20)
//        }

        

    }
    
    



    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
